using System;
using System.Collections.Generic;
using System.Text;
using com.paypal.sdk.util;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Common;

namespace Sedna.Payment.PayPal
{
    public abstract class PayPalAction : AbstractAction
    {
        private string api_Username = null;
        private string api_Password = null;
        private string api_Signature = null;
        /// <summary>
        ///  The PayPal environment (Live, Sandbox - using for test pay flow)
        /// </summary>
        private string environment = null;
        private string errorDetailsUnbindPath = null;
        private string onErrorStatus = "error";

        #region Properties
        public string Api_Username
        {
            get { return api_Username; }
            set { api_Username = value; }
        }

        public string Api_Password
        {
            get { return api_Password; }
            set { api_Password = value; }
        }

        public string Api_Signature
        {
            get { return api_Signature; }
            set { api_Signature = value; }
        }

        /// <summary>
        ///  The PayPal environment (Live, Sandbox - using for test pay flow)
        /// </summary>
        public string Environment
        {
            get { return environment; }
            set { environment = value; }
        }

        public string ErrorDetailsUnbindPath
        {
            get { return errorDetailsUnbindPath; }
            set { errorDetailsUnbindPath = value; }
        }

        public string OnErrorStatus
        {
            get { return onErrorStatus; }
            set { onErrorStatus = value; }
        }
        #endregion

        protected ActionResult CreateErrorResult(IExecutionContext executionContext, string nvpResponse)
        {
            NVPCodec decoder = new NVPCodec();
            decoder.Decode(nvpResponse);

            if (ErrorDetailsUnbindPath != null)
            {
                ErrorWraper wraper =
                    new ErrorWraper(decoder["L_ERRORCODE0"], decoder["L_SHORTMESSAGE0"], decoder["L_LONGMESSAGE0"]);
                executionContext.SetPropertyValue(ErrorDetailsUnbindPath, wraper);
            }
            return CreateResult(OnErrorStatus);
        }
    }
}
